﻿using BCCommon;
using BCDto.Location.Branches;
using BCDto.TMS.Agent.AgentBranchesConfig;
using BCService.TMS.Agent.AgentBranchesConfig;
using BCTMSRESTful.Configs;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BCTMSRESTful.Controllers.Management.TMS.AgentBranchesConfig
{
    /// <summary>
    /// 代理商网点配置
    /// </summary>
    [ApiExplorerSettings(GroupName = "TMSAdmin")]
    [Route("api/v1/sim/[controller]")]
    public class AgentBranchesConfigController : ManagementBaseController
    {
        private readonly IAgentBranchesConfigService agentBranchesConfigService;

        public AgentBranchesConfigController(IAgentBranchesConfigService agentBranchesConfigService)
        {
            this.agentBranchesConfigService = agentBranchesConfigService;
        }

        #region POST 添加  
        /// <summary>
        /// 添加新代理商网点配置记录
        /// </summary>
        /// <param name="dto">代理商网点配置数据模型</param>
        /// <returns></returns>
        [Log("添加新代理商网点配置记录")]
        [HttpPost]
        [ProducesResponseType(typeof(AgentBranchesConfigDto), 201)]
        public IActionResult Post([FromBody] AgentBranchesConfigRequestDto dto)
        {
            var result = agentBranchesConfigService.Add(this.Admin, dto);
            return Ok(result);
        }

        #endregion

        #region PUT 修改   
        /// <summary>
        /// 修改代理商网点配置记录
        /// </summary>
        /// <param name="agentBranchesConfigId">代理商网点配置ID</param>
        /// <param name="dto">代理商网点配置数据模型</param>
        /// <returns></returns>
        [Log("修改代理商网点配置记录")]
        [HttpPut("{agent_branches_config_id}")]
        [ProducesResponseType(typeof(AgentBranchesConfigDto), 200)]
        public IActionResult Put([FromRoute(Name = "agent_branches_config_id")]long agentBranchesConfigId,
            [FromBody] AgentBranchesConfigRequestDto dto)
        {
            var result = agentBranchesConfigService.Update(this.Admin, agentBranchesConfigId, dto);
            return this.Ok(result);
        }
        #endregion

        #region 删除  
        /// <summary>
        /// 删除代理商网点配置记录
        /// </summary>
        /// <param name="agentBranchesConfigId">代理商网点配置ID</param>
        /// <returns></returns>
        [Log("删除代理商网点配置记录")]
        [HttpDelete("{agent_branches_config_id}")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Delete([FromRoute(Name = "agent_branches_config_id")] long agentBranchesConfigId)
        {
            agentBranchesConfigService.Delete(this.Admin, agentBranchesConfigId);
            return NoContent();
        }
        #endregion

        #region GetByID     
        /// <summary>
        /// 获取一条代理商网点配置记录
        /// </summary>
        /// <param name="agentBranchesConfigId">代理商网点配置ID</param>
        /// <returns></returns>
        [HttpGet("{agent_branches_config_id}")]
        [ProducesResponseType(typeof(AgentBranchesConfigDto), 200)]
        public IActionResult Get([FromRoute(Name = "agent_branches_config_id")] long agentBranchesConfigId)
        {
            var result = agentBranchesConfigService.GetById(agentBranchesConfigId);
            return this.Ok(result);
        }
        #endregion

        #region Query 
        /// <summary>
        /// 查询代理商网点配置
        /// </summary>
        /// <param name="agentBranchesName">代理商网点名称</param>
        /// <param name="contactPhone">联系人电话</param>
        /// <param name="provinceCode">省份编码</param>
        /// <param name="cityCode">城市编码</param>
        /// <param name="countyCode">区县编码</param>
        /// <param name="address">地址信息</param>
        /// <param name="pageIndex">页号</param>
        /// <param name="pageSize">每页数据条数</param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<AgentBranchesConfigDto>), 200)]
        public IActionResult Query([FromQuery(Name = "agent_branches_name")]string agentBranchesName,
            [FromQuery(Name = "contact_phone")]string contactPhone,
            [FromQuery(Name = "province_code")]string provinceCode,
            [FromQuery(Name = "city_code")]string cityCode,
            [FromQuery(Name = "county_code")]string countyCode,
            [FromQuery(Name = "address")]string address,
            [FromQuery(Name = "page_index")] int pageIndex = 1, 
            [FromQuery(Name = "page_size")] int pageSize = 10)
        {
            var result = agentBranchesConfigService.Query(this.Admin, agentBranchesName, contactPhone, provinceCode, cityCode, countyCode, address, pageIndex, pageSize);
            return this.Ok(result);
        }
        #endregion

        #region List 
        /// <summary>
        /// 查询附近代理商网点配置
        /// </summary>
        /// <param name="longitude">经度</param>
        /// <param name="latitude">纬度</param>
        /// <param name="maxDistance">最大距离</param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<BranchesLocationNearDto>), 200)]
        public IActionResult List([FromQuery(Name = "longitude")]double longitude,
            [FromQuery(Name = "latitude")]double latitude,
            [FromQuery(Name = "max_distance")] int maxDistance)
        {
            var result = agentBranchesConfigService.GetList(longitude, latitude, maxDistance);
            return this.Ok(result);
        }
        #endregion
    }
}
